package com.example.rocketmqboot.controller;

import jakarta.annotation.Nullable;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.apache.rocketmq.spring.support.RocketMQHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @description: TODO
 * @author: uu
 * @date: 2023/12/16 21:04
 * @version: 1.0
 */
@RestController
public class RocketMQ {

    @Autowired
    private RocketMQTemplate template;

    @GetMapping("/")
    public String producerHandler(String msg, String topic, @Nullable String tag) {
        if (tag != null){
            topic = topic + ":" + tag;
        }
        SendResult res = template.syncSend(topic, msg);
        template.syncSend(topic, MessageBuilder.withPayload(msg)
                .setHeader(RocketMQHeaders.KEYS, "header_key")
                .build(), 3000, 2);
        return res.toString();
    }
}
